require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'RMagick'
include Magick

module ScraperHtml
  def clean_tag(html)

    doc = Hpricot(html,:xhtml_strict => true)

    doc.search("td span").each do |span|
      span.swap(span.inner_text)
    end

    doc.search("td p").each do |p|
      p.swap(p.inner_text)
    end

    doc.search("td div").each do |div|
      div.swap(div.inner_text)
    end

    doc.search("font").each do |font|
      font.swap(font.inner_text)
    end

    

    doc.search("*") do |tag|
      if tag.elem?
        tag.remove_attribute('class') if tag.has_attribute?('class')
        tag.remove_attribute('bgcolor') if tag.has_attribute?('bgcolor')
        tag.remove_attribute('style') if tag.has_attribute?('style')
        tag.remove_attribute('width') if tag.has_attribute?('width')
        tag.remove_attribute('height') if tag.has_attribute?('height')
      end
    end
    doc.search("table") do |table|
      table.remove_attribute('border') if table.has_attribute?('border')
    end

    
    return doc.to_html

  end

  def save_image(image_url,type_path,id)
    begin
      img = open(image_url){|f|f.read}
    rescue
      logger.error("can't open #{image_url}")
    else
      app_root = File.expand_path(RAILS_ROOT)
      img_extension = /\.([^.]+)$/.match(image_url).values_at(0).to_s
      image_dir = "#{app_root}/public/#{type_path}/image_url/#{id}"
      Dir.mkdir(image_dir,0775)
      image_path = "#{image_dir}/#{id}#{img_extension}"

      open(image_path,"wb"){|f|
        f.write(img)
      }
      #小图
      thumb = ImageList.new(image_path)
      thumb.resize_to_fit!(Product::THUMB_IMAGE_SIZE.to_i,Product::THUMB_IMAGE_SIZE.to_i)
      Dir.mkdir("#{File.dirname(image_path)}/thumb",0775)
      thumb.write("#{File.dirname(image_path)}/thumb/#{File.basename(image_path)}")
      #中图
      medium = ImageList.new(image_path)
      medium.resize_to_fit!(Product::MEDIUM_IMAGE_SIZE.to_i,Product::MEDIUM_IMAGE_SIZE.to_i)
      Dir.mkdir("#{File.dirname(image_path)}/medium",0775)
      medium.write("#{File.dirname(image_path)}/medium/#{File.basename(image_path)}")
      #大图
      ImageList.new(image_path).resize_to_fit!(Product::GEOMETRY_IMAGE_SIZE.to_i,Product::GEOMETRY_IMAGE_SIZE.to_i)
    end
    return image_path
  end

end